package com.yanyilu.qiubite.controller;

import com.yanyilu.qiubite.entity.AdminUser;
import com.yanyilu.qiubite.entity.Spot;
import com.yanyilu.qiubite.jwt.JwtIgnore;
import com.yanyilu.qiubite.service.AdminUserService;
import com.yanyilu.qiubite.service.SpotService;
import com.yanyilu.qiubite.service.StoryService;
import com.yanyilu.qiubite.util.PageUtil;
import com.yanyilu.qiubite.util.ParseJwtUtil;
import com.yanyilu.qiubite.util.R;
import com.yanyilu.qiubite.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("spot")
public class SpotController {
    @Autowired
    SpotService spotService;

    @Autowired
    AdminUserService adminUserService;

    @Autowired
    StoryService storyService;


    /**
     * 首页
     * @param appId
     * @return
     */
    @JwtIgnore
    @RequestMapping("/frontend/index")
    public R getIndex(@RequestParam("appId")String appId){

        Spot spot = spotService.findByAppId(appId);
        if(null != spot){
            return R.ok().put("spot", spot);
        }else{
            return R.error(ResultCode.DATA_IS_WRONG  );
        }
    }

    /**
     * 创建景区(同时创建该景区管理员)
     * @param spot
     * @param userName
     * @param password
     * @return
     */

    @PostMapping("/admin/save")
    public R save(@RequestBody Spot spot,
                  @RequestParam("userName") String userName,
                  @RequestParam("password") String password){
        String role = ParseJwtUtil.getRole();
        if("0".equals(role)){
            spotService.addSpot(spot,userName,password);
            return R.ok();

        }else {
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 景区列表
     * @param page
     * @param limit
     * @param spotName
     * @return
     */
    @GetMapping("/admin/list")
    public R getSpotList(@RequestParam("page")Integer page,
                         @RequestParam("limit")Integer limit,
                         @RequestParam(value = "spotName",required = false)String spotName){
        String role = ParseJwtUtil.getRole();
        if("0".equals(role)){
            Spot spot = new Spot();
            if(null == spotName || spotName.trim().length() == 0){
                spotName = null;
            }
            spot.setSpotName(spotName);
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("spotName", ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Spot> example = Example.of(spot,matcher);
            Pageable pageable = PageUtil.pagelimit(page, limit);
            Page<Spot> pages = spotService.getList(example,pageable);
            return R.ok().put("list",pages.getContent()).put("totalPages", pages.getTotalPages()).put("count", pages.getTotalElements());

        }else if("1".equals(role)){
            Integer adminUserId = ParseJwtUtil.getUserId();
            AdminUser adminUser = adminUserService.findUserById(adminUserId);

            Spot spot = new Spot();
            spot.setSpotId(adminUser.getSpotId());
            if(null == spotName || spotName.trim().length() == 0){
                spotName = null;
            }
            spot.setSpotName(spotName);
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("spotName", ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Spot> example = Example.of(spot,matcher);
            Pageable pageable = PageUtil.pagelimit(page, limit);
            Page<Spot> pages = spotService.getList(example,pageable);
            return R.ok().put("list",pages.getContent()).put("totalPages", pages.getTotalPages()).put("count", pages.getTotalElements());
        }else{
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 景区修改
     * @param spot
     * @return
     */
    @PostMapping("/admin/update")
    public R updateSpot(@RequestBody Spot spot){
        String role = ParseJwtUtil.getRole();
        if("0".equals(role)){
            spotService.updateSpot(spot);
            return R.ok();
        }else if("1".equals(role)){
            Integer userid = ParseJwtUtil.getUserId();
            Integer spotId = adminUserService.findSpotIdByUserId(userid);
            if(spotId.equals(spot.getSpotId())){
                spotService.updateSpot(spot);
                return R.ok();
            }else{
                return R.error(ResultCode.NO_ROLE_ERROR);
            }
        }else{
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 删除景区
     * @param spotId
     * @return
     */
    @RequestMapping("/admin/delete")
    public R delete(@RequestParam("spotId")Integer spotId){

        spotService.spotDelete(spotId);
        return R.ok();
    }

}
